package com.mrjy.schooldiy.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.DiyLableInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.enums.ResultEnum;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResultUtil;
import com.mrjy.maintain.util.RoleInfoEnum;
import com.mrjy.schooldiy.dao.IDiyLableInfoDao;

@Service("diyLableInfoBo")
public class DiyLableInfoBo implements IDiyLableInfoBo{
	@Resource
	private IDiyLableInfoDao diyLableInfoDao ;

	@Override
	public Object queryPageListForBack(DiyLableInfo info, HttpSession session) {
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo ui = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO) ;
		if(ui == null){
			return ResultUtil.error(ResultEnum.NOT_LOGIN);
		}
		if(ui.getRoleId() != null && 
			(ui.getRoleId().intValue() == RoleInfoEnum.SCHOOL_ADMIN.getRoleId()
				|| ui.getRoleId().intValue() == RoleInfoEnum.SERVICE_SCHOOL_ADMIN.getRoleId())){
			info.setSchoolId(ui.getSchoolId());
		}
		int count = diyLableInfoDao.countList(info) ;
		List<DiyLableInfo> list = diyLableInfoDao.queryPageList(info) ;
		map.put("total", count);
		map.put("rows", list);
		map.put("err_code", "200");
		map.put("err_msg", "查询成功");
		return map;
	}

	@Override
	public Object queryCanUseList(DiyLableInfo info) {
		Map< String, Object> map = new HashMap<String, Object>();
		int count = diyLableInfoDao.countList(info);
		List<DiyLableInfo> list = diyLableInfoDao.queryCanUseList(info) ;
		map.put("rows", list);
		map.put("err_code", "200");
		map.put("err_msg", "查询成功");
		map.put("total", count) ;
		return map;
	}

	@Override
	public Object queryListByName(DiyLableInfo dInfo) {
		Map< String, Object> map = new HashMap<String, Object>();
		List<DiyLableInfo> list = diyLableInfoDao.queryListByName(dInfo) ;
		map.put("rows", list);
		map.put("err_code", "200");
		map.put("err_msg", "查询成功");
		return map;
	}

	@Override
	public Object txUpdateDisableFlag(DiyLableInfo dInfo) {
		Map< String, Object> map = new HashMap<String, Object>();
		int count = diyLableInfoDao.updateDisableFlag(dInfo) ;
		map.put("count", count);
		map.put("err_code", "200");
		map.put("err_msg", "更新成功");
		return map;
	}

	@Override
	public Object txAdd(DiyLableInfo dInfo) {
		Map< String, Object> map = new HashMap<String, Object>();
		dInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		dInfo.setDisableFlag(0);
		int count = diyLableInfoDao.insert(dInfo) ;
		map.put("count", count);
		map.put("err_code", "200");
		map.put("err_msg", "新增成功");
		return map;
	}

	@Override
	public Object txEdit(DiyLableInfo dInfo) {
		Map< String, Object> map = new HashMap<String, Object>();
		int count = diyLableInfoDao.updateById(dInfo);
		map.put("count", count);
		map.put("err_code", "200");
		map.put("err_msg", "编辑成功");
		return map;
	}
	


}
